import {createWebHistory,createRouter} from 'vue-router'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs';
//路由配置
const routes = [
    {
        path:'/',
        redirect:'/login'
    },
    {
        path:'/login',
        name:'login',
        component:()=>import('@/view/Login.vue'),
    },
    {
        path:'/backend',
        name:'backend',
        component:()=>import('@/view/Backend.vue'),
        children:[
            {
                path:'/',
                redirect:'workbar',
            },
            {
                path:'workbar',
                name:'workbar',
                component:()=>import('@/component/Workbar.vue'),
            },
            {
                path:'file',
                children:[
                    {
                        path:'/',
                        redirect:'floor',
                    },
                    {
                        path:'floor',
                        component:()=>import('../component/FileManagement/FloorManagement.vue'),
                    },
                    {
                        path:'newFloor',
                        component:()=>import('../component/FileManagement/new/NewFloor.vue'),
                    },
                    {
                        path:'house',
                        component:()=>import('../component/FileManagement/HouseManagement.vue'),
                    },
                    {
                        path:'newHouse',
                        component:()=>import('../component/FileManagement/new/NewHouse.vue'),
                    },
                    {
                        path:'storage',
                        component:()=>import('../component/FileManagement/StorageManagement.vue'),
                    },
                    {
                        path:'newStorage',
                        component:()=>import('../component/FileManagement/new/NewStorage.vue'),
                    },
                    {
                        path:'household',
                        component:()=>import('../component/FileManagement/HouseholdManagement.vue'),
                    },
                    {
                        path:'newHousehold',
                        component:()=>import('../component/FileManagement/new/NewHousehold.vue'),
                    },
                    {
                        path:'houserent',
                        component:()=>import('../component/FileManagement/HouserentManagement.vue'),
                    },
                    {
                        path:'newHouserent',
                        component:()=>import('../component/FileManagement/new/NewHouserent.vue'),
                    },
                ]
            },
            {
                path:'car',
                children:[
                    {
                        path:'region',
                        component:()=>import('../component/CarManagement/RegionManagement.vue'),
                    },
                    {
                        path:'newRegion',
                        component:()=>import('../component/CarManagement/new/NewRegion.vue'),
                    },
                    {
                        path:'position',
                        component:()=>import('../component/CarManagement/PositionManagement.vue'),
                    },
                    {
                        path:'newPosition',
                        component:()=>import('../component/CarManagement/new/NewPosition.vue'),
                    },
                    {
                        path:'car',
                        component:()=>import('../component/CarManagement/CarManagement.vue'),
                    },
                    {
                        path:'newCar',
                        component:()=>import('../component/CarManagement/new/NewCar.vue'),
                    },
                    {
                        path:'payment',
                        component:()=>import('../component/CarManagement/PaymentManagement.vue'),
                    },
                    {
                        path:'newPayment',
                        component:()=>import('../component/CarManagement/new/NewPayment.vue'),
                    },
                ]
            },
            {
                path:'tenement',
                children:[
                    {
                        path:'repair',
                        component:()=>import('../component/TenementManagement/RepairManagement.vue'),
                    },
                    {
                        path:'newRepair',
                        component:()=>import('../component/TenementManagement/new/NewRepair.vue'),
                    },
                    {
                        path:'newActivity',
                        component:()=>import('../component/TenementManagement/new/NewActivity.vue'),
                    },
                    {
                        path:'activity',
                        component:()=>import('../component/TenementManagement/ActivityManagement.vue'),
                    },
                    {
                        path:'announce',
                        component:()=>import('../component/TenementManagement/AnnounceManagement.vue'),
                    },
                    {
                        path:'newAnnounce',
                        component:()=>import('../component/TenementManagement/new/Newannounce.vue'),
                    }
                ]
            },
            {
                path:'payment',
                children:[
                    {
                        path:'bill',
                        component:()=>import('../component/PaymentManagement/BillManagement.vue'),
                    },
                    {
                        path:'newBill',
                        component:()=>import('../component/PaymentManagement/new/NewBill.vue'),
                    },
                    {
                        path:'payItems',
                        component:()=>import('../component/PaymentManagement/PayItemsManagement.vue'),
                    },
                    {
                        path:'newPayItems',
                        component:()=>import('../component/PaymentManagement/new/NewPayItems.vue'),
                    },
                    {
                        path:'instrument',
                        component:()=>import('../component/PaymentManagement/InstrumentManagement.vue'),
                    },
                    {
                        path:'newInstrument',
                        component:()=>import('../component/PaymentManagement/new/NewInstrument.vue'),
                    }
                ]
            },
            {
                path:'patrol',
                children:[
                    {
                        path:'records',
                        component:()=>import('../component/PatrolManagement/PatrolRecordsManagement.vue'),
                    },
                    {
                        path:'task',
                        component:()=>import('../component/PatrolManagement/PatrolTaskManagement.vue'),
                    },
                    {
                        path:'newTask',
                        component:()=>import('../component/PatrolManagement/new/NewPatrolTask.vue'),
                    },
                    {
                        path:'plan',
                        component:()=>import('../component/PatrolManagement/PatrolPlanManagement.vue'),
                    },
                    {
                        path:'newPlan',
                        component:()=>import('../component/PatrolManagement/new/NewPatrolPlan.vue'),
                    },
                    {
                        path:'point',
                        component:()=>import('../component/PatrolManagement/PatrolPointManagement.vue'),
                    },
                    {
                        path:'newPoint',
                        component:()=>import('../component/PatrolManagement/new/NewPatrolPoint.vue'),
                    },
                    {
                        path:'path',
                        component:()=>import('../component/PatrolManagement/PatrolPathManagement.vue'),
                    },
                    {
                        path:'newPath',
                        component:()=>import('../component/PatrolManagement/new/NewPatrolPath.vue'),
                    },
                    {
                        path:'project',
                        component:()=>import('../component/PatrolManagement/PatrolProjectManagement.vue'),
                    },
                    {
                        path:'newProject',
                        component:()=>import('../component/PatrolManagement/new/NewPatrolProject.vue'),
                    },
                    {
                        path:'daily',
                        component:()=>import('../component/PatrolManagement/PatrolDailyManagement.vue'),
                    },
                    {
                        path:'weekly',
                        component:()=>import('../component/PatrolManagement/PatrolWeeklyManagement.vue'),
                    },
                    {
                        path:'monthly',
                        component:()=>import('../component/PatrolManagement/PatrolMonthlyManagement.vue'),
                    },
                    {
                        path:'business',
                        component:()=>import('../component/PatrolManagement/PatrolBusinessManagement.vue'),
                    }
                ]
            },
            {
                path:'equipment',
                children:[
                    {
                        path:'public',
                        component:()=>import('../component/EquipmentManagement/PublicManagement.vue'),
                    },
                    {
                        path:'newDevice',
                        component:()=>import('../component/EquipmentManagement/new/NewDevice.vue'),
                    },
                    {
                        path:'classify',
                        component:()=>import('../component/EquipmentManagement/ClassifyManagement.vue'),
                    },
                    {
                        path:'newGroup',
                        component:()=>import('../component/EquipmentManagement/new/NewGroup.vue'),
                    },
                    {
                        path:'repair',
                        component:()=>import('../component/EquipmentManagement/RepairManagement.vue'),
                    },
                    {
                        path:'newRepair',
                        component:()=>import('../component/EquipmentManagement/new/NewRepair.vue'),
                    }
                ]
            },
            {
                path:'setting',
                children:[
                    {
                        path:'householdLabel',
                        component:()=>import('../component/Setting/HouseholdLabel.vue'),
                    },
                    {
                        path:'newHouseholdLabel',
                        component:()=>import('../component/Setting/new/NewHouseholdLabel.vue'),
                    },
                    {
                        path:'houseFacility',
                        component:()=>import('../component/Setting/HouseFacility.vue'),
                    },
                    {
                        path:'newHouseFacility',
                        component:()=>import('../component/Setting/new/NewHouseFacility.vue'),
                    },
                    {
                        path:'employee',
                        component:()=>import('../component/Setting/Employee.vue'),
                    },
                    {
                        path:'newEmployee',
                        component:()=>import('../component/Setting/new/NewEmployee.vue'),
                    },
                    {
                        path:'character',
                        component:()=>import('../component/Setting/Character.vue'),
                    },
                    {
                        path:'newCharacter',
                        component:()=>import('../component/Setting/new/NewCharacter.vue'),
                    },

                ]
            }
        ]
    }
]

const router = createRouter({
    history:createWebHistory(),
    routes: routes,
})
//前端路由配置
// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  NProgress.start();  // 启动进度条

  const token = localStorage.getItem('token');  // 获取 token

  // 如果目标路由是登录页，直接放行
  if (to.name === 'Login') {
    next();
  } else {
    // 如果没有 token 且目标路由不是登录页，跳转到登录页
    if (!token) {
      next({ name: 'Login' });  // 强制跳转到登录页
    } else {
      next();  // 有 token，正常跳转
    }
  }
});

router.afterEach(()=>{
    NProgress.done();
})
//抛出路由
export default router